package com.bdd.controller;

import com.bdd.Result.Result;
import com.bdd.model.bo.PageBO;
import com.bdd.model.bo.clothing.ClothingBO;
import com.bdd.model.bo.clothing.ClothingAddBO;
import com.bdd.service.ClothingService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author huanghaining
 * @date 2022/3/18
 */
@RestController
@RequestMapping("/clothing")
@Api(value = "商品管理")
public class ClothingController {

    @Autowired
    ClothingService clothingService;

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("服装列表")
    public Result<PageInfo<ClothingBO>> list(PageBO pageBO){
        if(pageBO.getPageNum()<=0){
            pageBO.setPageNum(1);
        }
        if(pageBO.getPageSize()<=0){
            pageBO.setPageSize(10);
        }
        PageInfo<ClothingBO> ClothingBOList = new PageInfo<>(clothingService.list(pageBO.getPageNum(),pageBO.getPageSize()));
        return Result.ok(ClothingBOList);
    }

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("增加服装")
//    @RequiresRoles({"ADMIN"})
    public Result add(ClothingAddBO bo){
        clothingService.add(bo);
        return Result.ok();
    }

    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation("编辑服装")
//    @RequiresRoles({"ADMIN"})
    public Result edit(ClothingBO bo){
        if(Objects.isNull(bo.getId())){
            throw new IllegalArgumentException("服装id不可为空");
        }
        clothingService.edit(bo);
        return Result.ok();
    }

    @PostMapping("/delete")
    @ResponseBody
    @ApiOperation("删除服装")
    @RequiresRoles({"ADMIN"})
    public Result delete(Integer id){
        if(Objects.isNull(id)){
            throw new IllegalArgumentException("服装id不可为空");
        }
        clothingService.delete(id);
        return Result.ok();
    }

    @GetMapping("/search")
    @ResponseBody
    @ApiOperation("根据服装名称搜索")
    @RequiresRoles({"ADMIN"})
    public Result<List<ClothingBO>> searchByName(String name){
        List<ClothingBO> ClothingBOList = clothingService.searchByName(name);
        return Result.ok(ClothingBOList);
    }




}
